package com.cv.platform.dal.mapper.history;

import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.mybatis.core.mapper.BaseMapperX;
import com.cv.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.platform.controller.platform.history.vo.CvVideoHistoryRespVO;
import com.cv.platform.controller.platform.history.vo.CvVideoHistoryReqVO;
import com.cv.platform.dal.dataobject.history.CvVideoHistoryRecordDO;
import com.cv.platform.dal.dataobject.video.CvVideoDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * 视频播放历史记录数据层
 *
 * @author Charles_XDXD
 */
@Mapper
public interface CvVideoHistoryRecordMapper extends BaseMapperX<CvVideoHistoryRecordDO> {

    default PageResult<CvVideoHistoryRespVO> selectPage(CvVideoHistoryReqVO reqVO) {
        return selectJoinPage(reqVO, CvVideoHistoryRespVO.class, new MPJLambdaWrapperX<CvVideoHistoryRecordDO>()
                .selectAll(CvVideoHistoryRecordDO.class)
                .eq(CvVideoHistoryRecordDO::getUserId, SecurityFrameworkUtil.getLoginUserId())
                .leftJoin(CvVideoDO.class, CvVideoDO::getId, CvVideoHistoryRecordDO::getVideoId)
                .selectAs(CvVideoDO::getUserId, CvVideoHistoryRespVO::getUserId)
                .selectAs(CvVideoDO::getThumbnail, CvVideoHistoryRespVO::getThumbnail)
                .selectAs(CvVideoDO::getTitle, CvVideoHistoryRespVO::getTitle)
                .selectAs(CvVideoDO::getViewCount, CvVideoHistoryRespVO::getViewCount)
                .selectAs(CvVideoDO::getCreateTime, CvVideoHistoryRespVO::getPublishTime)
                .likeIfExists(CvVideoDO::getTitle, reqVO.getTitle())
                .orderByDesc(CvVideoHistoryRecordDO::getUpdateTime)
        );
    }

}
